/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       InitMngTablesService.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/11/19      feiwe                         add
 */


package com.fengwei.service;

import cn.hutool.core.io.resource.ResourceUtil;
import com.fengwei.util.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;

@Service
public class InitMngTablesService {
    private final Logger logger = LoggerFactory.getLogger(InitMngTablesService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private Environment env;

    public void initMngTables() throws Exception {
        String driverClassName = env.getProperty("spring.datasource.driver-class-name");
        switch (driverClassName) {
            case "org.h2.Driver":
                createH2Tables();
                break;
            default:
                break;
        }
    }

    private void createH2Tables() throws Exception {
        createH2TableAndIndex("mdms_database");
        createH2TableAndIndex("mdms_datachange_apply");
        createH2TableAndIndex("mdms_user");
        createH2TableAndIndex("mdms_user_commonsql");
        createH2TableAndIndex("mdms_user_database");
        createH2TableAndIndex("mdms_user_log");
        createH2TableAndIndex("mdms_user_privilige");
    }

    private void createH2TableAndIndex(String tableName) throws IOException {
        String checkTable = "select table_name from information_schema.tables where table_schema='PUBLIC' and table_name = '" + tableName.toUpperCase() + "'";
        List tableList = jdbcTemplate.queryForList(checkTable);
        logger.debug(GsonUtil.toJson(tableList, false));

        /*表不存在时就创建表*/
        if (tableList.size() == 0) {
            String result = ResourceUtil.readUtf8Str("tables/h2/" + tableName + ".sql");
            logger.debug("result:" + result);
            StringTokenizer stn = new StringTokenizer(result, ";");
            while (stn.hasMoreTokens()) {
                String sql = stn.nextToken();
                sql = sql.trim().replaceAll("\r", "").replaceAll("\n", "");
                //logger.debug("sql:"+sql);
                if (sql.startsWith("create") || sql.startsWith("insert")) {
                    jdbcTemplate.execute(sql);
                }
            }
        }


    }

    private void createMysqlTableAndIndex(String tableName) throws FileNotFoundException {
//        String checkTable = "show tables like 'userbrandtime'";
//        Map a = jdbcTemplate.queryForMap(checkTable);
//        logger.debug(GsonUtil.toJson(a,false));


    }
}
